package com.efuture.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @ClassNAME JdbcLoginCaseTest
 * @Description 使用JDBC实现一个简单的登录案例
 * @Author zhaoshanshan
 * @Date 2021/9/19 9:20 下午
 * @Version 1.0
 */
public class JdbcLoginCaseTest {

    public static void main(String[] args) {
        //键盘录入
        System.out.println("请输入用户名:");
        Scanner scanner = new Scanner(System.in);
        String userName = scanner.nextLine();
        System.out.println("请输入密码");
        String passWord = scanner.nextLine();
        JdbcLoginCaseTest jdbcLoginCaseTest = new JdbcLoginCaseTest();
        boolean flag = jdbcLoginCaseTest.login(userName,passWord);
        if(!flag){
            System.out.println("登录失败");
        }else{
            System.out.println("登录成功");
        }
    }
    private boolean login(String userName,String passWord){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rst = null;
        try{
            conn = JDBCUtils.getConnection();
            stmt = conn.createStatement();
            String sql = "select * from user where username = '"+userName+"' AND PASSWORD = '"+passWord+"'";
            rst = stmt.executeQuery(sql);
            return rst.next();
        }catch (Exception e){
            e.printStackTrace();;
        }finally {
            JDBCUtils.close(rst,conn,stmt);
        }
        return false;

    };

}
